<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>教师管理</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .cont {
            padding: 10px;
        }

        #xz {
            margin-bottom: 10px;
        }

        #tbody td {
            line-height: 2.42857143;
        }

        #tbody td>button:first-child {
            margin-right: 10px;
        }

        #tc {
            position: absolute;
            width: calc(100vw - 20px);
            height: 100vh;
            top: 0px;
            background-color: white;
            display: none;
        }

        #tc>button {
            margin-right: 10px;
        }

        .xial {
            display: block;
            border: 1px solid #ccc;
            padding: 6px 12px;
            font-size: 14px;
            line-height: 1.42857143;
            border-radius: 4px;
            color: #555;
            background-color: #fff;
        }
    </style>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css"
        integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"
        integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd"
        crossorigin="anonymous"></script>
</head>

<body>
    <div class="cont">
        <button class="btn btn-primary m-r-5" id="xz" onclick="tC()" value="51">新增教师</button>
        <table class="table table-striped">
            <thead>
                <tr>
                    <th class="text-center">姓名</th>
                    <th class="text-center">性别</th>
                    <th class="text-center">年龄</th>
                    <th class="text-center">地址</th>
                    <th class="text-center">毕业院校</th>
                    <th class="text-center">专业</th>
                    <th class="text-center">角色</th>
                    <th class="text-center">职称</th>
                    <th class="text-center" id="cZ">操作</th>
                </tr>
            </thead>
            <tbody class="text-center" id="tbody">
            </tbody>
            <tfoot class="text-center">
                <tr>
                    <td colspan="9">
                        <nav aria-label="Page navigation">
                            <ul class="pagination">
                                <li onclick="syy()">
                                    <a href="#" aria-label="Previous">
                                        <span aria-hidden="true">&laquo;</span>
                                    </a>
                                </li>
                                <li><a href="#" id="yema">1</a></li>
                                <li onclick="xyy()">
                                    <a href="#" aria-label="Next">
                                        <span aria-hidden="true">&raquo;</span>
                                    </a>
                                </li>
                            </ul>
                        </nav>
                    </td>
                </tr>
            </tfoot>
        </table>
        <form id="tc" onsubmit="return false;">
            <div class="h3" id="mc">新增教师</div>
            <div class="form-group">
                <label for="exampleInputEmail1">教师名称：</label>
                <input type="text" class="form-control" placeholder="教师名称" id="mingC">
            </div>
            <div class="form-group">
                <label for="exampleInputPassword1">性别：</label>
                <select name="" class="xial">
                    <option value="">请选择性别</option>
                    <option value="男">男</option>
                    <option value="女">女</option>
                </select>
            </div>
            <div class="form-group">
                <label for="exampleInputEmail1">年龄：</label>
                <input type="text" class="form-control" placeholder="年龄" id="age">
            </div>
            <div class="form-group">
                <label for="exampleInputEmail1">地址：</label>
                <input type="text" class="form-control" placeholder="地址" id="address">
            </div>
            <div class="form-group">
                <label for="exampleInputFile">毕业院校：</label>
                <input type="text" class="form-control" placeholder="毕业院校" id="yuanX">
            </div>
            <div class="form-group">
                <label for="exampleInputEmail1">专业：</label>
                <input type="text" class="form-control" placeholder="专业" id="zhuanY">
            </div>
            <div class="form-group" id="sjh">
                <label for="exampleInputEmail1">手机号：</label>
                <input type="text" class="form-control" placeholder="手机号" id="phone">
            </div>
            <div class="form-group">
                <label for="exampleInputEmail1">角色：</label>
                <select name="" class="xial">
                </select>
            </div>
            <div class="form-group">
                <label for="exampleInputEmail1">职称：</label>
                <input type="text" class="form-control" placeholder="职称" id="zhicheng">
            </div>
            <button class="btn btn-primary m-r-5" onclick="xZ()" id="anmc">新增</button><button
                class="btn btn-primary m-r-5" onclick="qX()">取消</button>
        </form>
    </div>
    <script src="../js/pubilc.js"></script>
    <script>
        var pd = -1
        var fly = -1
        var item = 1
        var y = 1
        var iD = ''
        var xial = document.getElementsByClassName('xial')
        bd()
        function bd() {
            xhr({
                methods: 'get',
                url: `/user/info?token=${sessionStorage.getItem('token')}`,
                success: function (res) {
                    if (res.role_id != null) {
                        xhr({
                            methods: 'post',
                            url: `/roleSys/getRoleList`,
                            data: JSON.stringify({
                                role_school_id: Number(sessionStorage.getItem('id'))
                            }),
                            success: function (fly) {
                                for (i = 0; i < fly.length; i++) {
                                    if (res.role_id == fly[i].role_id) {
                                        var index = fly[i].role_rank_id
                                        xhr({
                                            methods: 'get',
                                            url: `/rankSys/getRank?school_id=${Number(sessionStorage.getItem('id'))}`,
                                            success: function (l) {
                                                for (j = 0; j < l.length; j++) {
                                                    if (l[j].rank_id == index) {
                                                        var ind = xz.value.split('')[0] - 1
                                                        var chaiji
                                                        if (l[j].rank_id == index) {
                                                            var ind = xz.value.split('')[0] - 1
                                                            var chaiji
                                                            if (l[j].rank_sys.length < ind) {
                                                                xz.style.display = 'none'
                                                                 cZ.style.display = 'none'
                                                                 for (i = 0; i < anN.length; i++) {
                                                                    if (anN[i].value == 52) {
                                                                        anN[i].style.display = 'none'
                                                                    }
                                                                }
                                                                for (j = 0; j < anN.length; j++) {
                                                                    if (anN[j].value == 53) {
                                                                        anN[j].style.display = 'none'
                                                                    }
                                                                }
                                                                return
                                                            } else {
                                                                chaiji = l[j].rank_sys[ind].page_function_ids.split(',')
                                                            }
                                                            console.log(chaiji)
                                                            if (chaiji.indexOf('51') == -1) {
                                                                xz.style.display = 'none'
                                                            }
                                                            if (chaiji.indexOf('52') == -1 && chaiji.indexOf('53') == -1) {
                                                                cZ.style.display = 'none'
                                                            }
                                                            if (chaiji.indexOf('52') == -1) {
                                                                for (i = 0; i < anN.length; i++) {
                                                                    if (anN[i].value == 52) {
                                                                        anN[i].style.display = 'none'
                                                                    }
                                                                }
                                                            }
                                                            if (chaiji.indexOf('53') == -1) {
                                                                for (j = 0; j < anN.length; j++) {
                                                                    if (anN[j].value == 53) {
                                                                        anN[j].style.display = 'none'
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }

                                                }

                                            }
                                        })
                                    }
                                }
                            }
                        })
                    } else {
                        return
                    }
                }
            })
        }
        function js() {
            xhr({
                methods: 'post',
                url: `/roleSys/getRoleList`,
                data: JSON.stringify({
                    role_school_id: Number(sessionStorage.getItem('id'))
                }),
                success: function (res) {
                    console.log(res)
                    xial[1].innerHTML = '<option value="">请选择角色</option>'
                    for (i = 0; i < res.length; i++) {
                        xial[1].innerHTML += `<option value="${res[i].role_id}">${res[i].role_name}</option>`
                    }
                }
            })
        }
        function tC() {
            sjh.style.display = 'block'
            tc.style.display = 'block'
            mc.innerHTML = '新增教师'
            anmc.innerHTML = '新增'
            fly = -1
            js()
        }
        function xg(i, j) {
            js()
            iD = j
            sjh.style.display = 'none'
            tc.style.display = 'block'
            mc.innerHTML = '修改教师'
            anmc.innerHTML = '修改'
            fly = 1
            xhr({
                methods: 'post',
                url: '/user/getUserList',
                data: JSON.stringify({
                    page: item,
                    size: 10,
                    school_id: Number(sessionStorage.getItem('id')),
                    identity: '2',
                }),
                success: function (res) {
                    console.log(res)
                    mingC.value = res.list[i].user_name
                    zhuanY.value = res.list[i].specialty
                    yuanX.value = res.list[i].endSchool
                    age.value = res.list[i].user_age
                    address.value = res.list[i].user_address
                    zhicheng.value = res.list[i].level_title
                    xial[0].value = res.list[i].user_sex
                    xial[1].value = res.list[i].role_id
                }
            })

        }
        function qX() {
            if (fly == -1) {
                phone.value = ''
            } else {
                console.log('修改')
            }
            tc.style.display = 'none'
            mingC.value = ''
            zhuanY.value = ''
            yuanX.value = ''
            age.value = ''
            address.value = ''
            zhicheng.value = ''
            xial[0].value = ''
            xial[1].value = ''
        }
        // function sc(i) {
        //     arr.splice(i, 1)
        //     biaoG()
        // }
        function xZ() {
            pd = -1
            if (fly == -1) {
                if (pd == -1) {
                    xhr({
                        methods: 'post',
                        url: '/user/addUser',
                        data: JSON.stringify({
                            identity: '2',
                            role_id: Number(xial[1].value),
                            specialty: zhuanY.value,
                            endSchool: yuanX.value,
                            user_name: mingC.value,
                            user_sex: xial[0].value,
                            user_age: age.value,
                            user_address: address.value,
                            level_title: zhicheng.value,
                            teacher_phone: phone.value,
                            belong_to_school: Number(sessionStorage.getItem('id'))
                        }),
                        success: function (res) {
                            console.log(res)
                            pd = 1
                            biaoG()
                            qX()
                        }
                    })
                } else if (pd == 1) {
                    return
                }
            } else {
                if (pd == -1) {
                    xhr({
                        methods: 'post',
                        url: '/user/editTeacher',
                        data: JSON.stringify({
                            identity: '2',
                            user_id: iD,
                            role_id: Number(xial[1].value),
                            specialty: zhuanY.value,
                            endSchool: yuanX.value,
                            user_name: mingC.value,
                            user_sex: xial[0].value,
                            user_age: age.value,
                            user_address: address.value,
                            level_title: zhicheng.value,
                            teacher_phone: phone.value,
                            belong_to_school: Number(sessionStorage.getItem('id'))
                        }),
                        success: function (res) {
                            console.log(res)
                            pd = 1
                            biaoG()
                            qX()
                        }
                    })
                } else if (pd == 1) {
                    return
                }
            }
        }
        biaoG()
        function biaoG() {
            xhr({
                methods: 'post',
                url: '/user/getUserList',
                data: JSON.stringify({
                    page: item,
                    size: 10,
                    school_id: Number(sessionStorage.getItem('id')),
                    identity: '2',
                }),
                success: function (res) {
                    console.log(res)
                    tbody.innerHTML = ''
                    yema.innerHTML = y
                    for (i = 0; i < res.list.length; i++) {
                        tbody.innerHTML += `<tr>
                        <td>${res.list[i].user_name}</td>
                        <td>${res.list[i].user_sex}</td>
                        <td>${res.list[i].user_age}</td>
                        <td>${res.list[i].user_address}</td>
                        <td>${res.list[i].endSchool}</td>
                        <td>${res.list[i].specialty}</td>
                        <td class="jueS">${a(res.list[i].role_id, i)}</td>
                        <td>${res.list[i].level_title}</td>
                        <td><button class="btn btn-primary m-r-5" onclick='xg(${i},${res.list[i].user_id})' value="52">修改</button><button class="btn btn-primary m-r-5" onclick='sc(${i})' value="53">删除</button></td>
                    </tr>`
                        function a(fly, j) {
                            var jueS = document.getElementsByClassName('jueS')
                            xhr({
                                methods: 'post',
                                url: `/roleSys/getRoleList`,
                                data: JSON.stringify({
                                    role_school_id: Number(sessionStorage.getItem('id'))
                                }),
                                success: function (res) {
                                    for (i = 0; i < res.length; i++) {
                                        if (res[i].role_id == fly) {
                                            jueS[j].innerHTML = res[i].role_name
                                        }
                                    }
                                }
                            })
                        }
                    }
                }
            })

        }
        var anN = document.getElementsByTagName('button')
        function syy() {
            if (item <= 1) {
                item = 1
                y = 1
                return
            }
            item--
            y--
            biaoG()
        }
        function xyy() {
            item++
            y++
            biaoG()
        }
    </script>
</body>

</html>